<?php
namespace App\Domain;

use App\Common\Functions;
use Platform\Model\Order as OrderModel;
use Platform\Model\SystemGoods as SystemGoodsModel;
use Platform\Model\SystemStore as SystemStoreModel;
use PhalApi\Exception\BadRequestException;

class Order extends Functions{
    //获取订单列表
    public function orderList($where,$select,$order,$page,$perpage) {
        $model = new OrderModel();
        //处理筛选条件
        $where['so.delete_flag'] = 'N';
        $where['so.member_id'] = $this->tokenData['user_id'];
        $count = $model->getOrderCount($where,array(),'so.id');
        $list = $model->getOrderList($where,$select,$order,array(),$page,$perpage);
        $data['items'] = $list;
        $data['total'] = $count;
        return  $data;
    }
    //下单
    public function confirmOrder($goods_id,$order_price){
        $model = new OrderModel();
        //查询相应的商品信息
        $SystemGoodsModel = new SystemGoodsModel();
        $SystemStoreModel = new SystemStoreModel();
        $goodsData = $SystemGoodsModel->getData(array('id'=>$goods_id),array(),'delete_flag,store_id,now_price,delete_flag,level_num,price_level');
        if( empty($goodsData)&&$goodsData['delete_flag']=='Y' ){
            throw new BadRequestException('商品不存在或已删除！');
        }
        //查询专场信息
        $storeData = $SystemStoreModel->getData(array('id'=>$goodsData['store_id']),array(),'id,delete_flag,status');
        if( empty($storeData)&&$storeData['delete_flag']=='Y' ){
            throw new BadRequestException('专场不存在或已删除！');
        }
        if( $storeData['status']!='10' ){
            throw new BadRequestException('专场不在拍卖中，请返回首页！');
        }
        //添加信息
        $insertData = array();
        //处理订单号
        $order_count = $model->count(array('delete_flag'=>'N'))+1;
        $str = date('Ymd');
        if( $order_count == 1 ){
            $str .= '00001';
        }elseif( $order_count < 10 ){//1位
            $str .= '0000'.$order_count;
        }elseif( $order_count>9 && $order_count<100 ){//2位
            $str .= '000'.$order_count;
        }elseif( $order_count>99 && $order_count<1000 ){//3位
            $str .= '00'.$order_count;
        }elseif( $order_count>999 && $order_count<10000 ){//4位
            $str .= '0'.$order_count;
        }elseif( $order_count>9999 && $order_count<100000 ){//5位
            $str .= $order_count;
        }
        $insertData['order_no'] = 'D'.$str;
        $insertData['member_id'] = $this->tokenData['user_id'];
        $insertData['member_mobile'] = $this->tokenData['member_mobile'];
        $insertData['member_name'] = $this->tokenData['member_name'];
        $insertData['goods_id'] = $goods_id;
        $insertData['store_id'] = $storeData['id'];
        $insertData['order_price'] = $order_price;
        $insertData = $this->selfAddFields($insertData);
        $order_info = $model->getData(array('delete_flag'=>'N','goods_id'=>$this->tokenData['goods_id'],'order_price'=>$insertData['order_price'],$this->tokenData['user_id']));
        if( !empty( $order_info ) ){
            throw new BadRequestException('当前出价价格已经存在，请刷新后重试！');
        }
        //查询当前订单价格是否已经存在
//        $order_info = $model->getData(array('delete_flag'=>'N','goods_id'=>$this->tokenData['goods_id']));
//        if( !empty( $order_info ) ){
//            $order_info = $model->getData(array('delete_flag'=>'N','goods_id'=>$this->tokenData['goods_id'],'order_price'=>$insertData['order_price']));
//            if( !empty( $order_info ) ){
//                $insertData['order_price'] = $insertData['order_price']+$goodsData['price_level'];
//            }else{
//                $order_info = $model->getData(array('delete_flag'=>'N','goods_id'=>$this->tokenData['goods_id'],'order_price'=>$goodsData['now_price']));
//                if( empty( $order_info ) ){
//                    throw new BadRequestException('当前出价价格不符，请联系管理员！');
//                }
//            }
//        }
        //因为涉及数据表多步操作，所以要开启事务
        \PhalApi\DI()->notorm->beginTransaction('db_master');
        $res = $model->insert($insertData);
        if( $res === false ){
            \PhalApi\DI()->notorm->rollback('db_master');//回滚
            throw new BadRequestException('出价错误，请联系管理员！');
        }
        //更新当前商品的出价数和最新价格
        $updateData = array();
        $updateData['now_price'] = $insertData['order_price'];
        $updateData['level_num'] = new \NotORM_Literal("level_num + 1");
        $updateData = $this->selfEditFields($updateData);
        $res = $SystemGoodsModel->update($goods_id,$updateData);
        if( $res === false ){
            \PhalApi\DI()->notorm->rollback('db_master');//回滚
            throw new BadRequestException('出价错误，请联系管理员！');
        }
        \PhalApi\DI()->notorm->commit('db_master');//提交
        return $res;
    }
}
